package net.huanger.gametools.mapeditor.ui;


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JToolBar;

import net.huanger.gametools.mapeditor.model.MapModel;


public class MapEditorPanel extends JPanel
{
  private static final long serialVersionUID = 1L;

  private MapCanvas mapCanvas;

  private TitePicCanvas titePicCanvas;

  private JSplitPane splitPane;

  private JToolBar toolBar;

  private JRadioButton puzzleBtn;

  private JRadioButton blockBtn;

  private JRadioButton eventBtn;

  private JCheckBox titeDiv;

  private JCheckBox blockDiv;
  public static JLabel TITELABEL;
  private JCheckBox eventDiv;
  private JCheckBox guidesDiv;
  private List<JComponent> dependMap=new ArrayList<JComponent>();
  private OperateContext oc;
  public MapEditorPanel()
  {
    init();
  }

  private void init()
  {
    initModel();
    initComponent();
    initEventListener();
    for(JComponent jc:dependMap){
      jc.setEnabled(false);
    }
  }

  private void initModel()
  {
  }

  private void initComponent()
  {
    toolBar = new JToolBar();
    puzzleBtn = new JRadioButton("拼图");
    dependMap.add( puzzleBtn);
    blockBtn = new JRadioButton("遮挡");
    dependMap.add(blockBtn);
    eventBtn = new JRadioButton("事件");
    dependMap.add(eventBtn);
    ButtonGroup bg = new ButtonGroup();
    bg.add(puzzleBtn);
    bg.add(blockBtn);
    bg.add(eventBtn);
    toolBar.add(puzzleBtn);
    toolBar.add(blockBtn);
    toolBar.add(eventBtn);
    toolBar.addSeparator();
    toolBar.addSeparator();

    titeDiv=new JCheckBox("TiteDiv");
    dependMap.add(titeDiv);
    blockDiv=new JCheckBox("BlockDiv");
    dependMap.add( blockDiv);
    eventDiv=new JCheckBox("EventDiv");
    dependMap.add(eventDiv);
    guidesDiv=new JCheckBox("GuidesDiv");
    dependMap.add(guidesDiv);
    toolBar.add(titeDiv);
    toolBar.add(blockDiv);
    toolBar.add(eventDiv);
    toolBar.add(guidesDiv);
    
    toolBar.addSeparator();
    toolBar.addSeparator();
    TITELABEL=new JLabel(new ImageIcon(MUtils.BLOCK_IMG));
    toolBar.add(TITELABEL);
    
    titePicCanvas = new TitePicCanvas(this);
    mapCanvas = new MapCanvas(this);
    //    titePicCanvas.setMapCanvas(mapCanvas);

    splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    splitPane.setOneTouchExpandable(true);
    splitPane.setLeftComponent(new JScrollPane(titePicCanvas));
    splitPane.setRightComponent(new JScrollPane(mapCanvas));

    this.setLayout(new BorderLayout());
    this.add(toolBar, BorderLayout.NORTH);
    this.add(splitPane, BorderLayout.CENTER);
  }
  public void setDiv(int width){
    splitPane.setDividerLocation(width);
  }
  private void initEventListener()
  {
    //选择要显示的图层
    ActionListener selectDivListener=new ActionListener()
      {

        @Override
        public void actionPerformed(ActionEvent e)
        {
          if(e.getSource().equals(eventDiv)){
            oc.getEventDiv().isShow(eventDiv.isSelected());
          }else if(e.getSource().equals(titeDiv)){
            oc.getTiteDiv().isShow(titeDiv.isSelected());
          }else if(e.getSource().equals(blockDiv)){
           oc.getBlockDiv().isShow(blockDiv.isSelected());
          }else if(e.getSource().equals(guidesDiv)){
            oc.getGuidesDiv().isShow(guidesDiv.isSelected());
          }
          mapCanvas.repaint();
        }
      };
    
    eventDiv.addActionListener(selectDivListener);
    titeDiv.addActionListener(selectDivListener);
    blockDiv.addActionListener(selectDivListener);
    guidesDiv.addActionListener(selectDivListener);
    
    //选择要进行的操作
    ActionListener selectOperateListener=new ActionListener()
      {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource().equals(puzzleBtn)&&puzzleBtn.isSelected()){
             oc.selectPuzzle(); 
            }else if(e.getSource().equals(blockBtn)&&blockBtn.isSelected()){
              oc.selectBlock();
            }else if(e.getSource().equals(eventBtn)&&eventBtn.isSelected()){
              oc.selectEvent();
            }
        }
      };
    puzzleBtn.addActionListener(selectOperateListener);
    blockBtn.addActionListener(selectOperateListener);
    eventBtn.addActionListener(selectOperateListener);
  }

  public MapCanvas getMapCanvas()
  {
    return mapCanvas;
  }

  public void setMapCanvas(MapCanvas mapCanvas)
  {
    this.mapCanvas = mapCanvas;
  }

  public TitePicCanvas getTitePicCanvas()
  {
    return titePicCanvas;
  }

  public void setTitePicCanvas(TitePicCanvas titePicCanvas)
  {
    this.titePicCanvas = titePicCanvas;
  }

  public OperateContext getOperateContext()
  {
    return oc;
  }

  public void createMap(int row, int column,MapModel mm)
  {
    oc=new OperateContext(row,column);
    oc.initMapData(mm);
    mapCanvas.setPreferredSize(new Dimension(column*MUtils.MAP_UNIT, row*MUtils.MAP_UNIT));
    for(JComponent jc:dependMap){
      jc.setEnabled(true);
      if(jc instanceof JCheckBox){
        ((JCheckBox)jc).setSelected(true);
      }
    }
    mapCanvas.revalidate();
    mapCanvas.repaint();
  }

  public void importMap(File f) throws FileNotFoundException
  {
    if(titePicCanvas.getTiteImage()==null){
      JOptionPane.showMessageDialog(this, "必须先导入TitePic！");
    }else{
      MapModel mm = MUtils.loadMapData(f);
      createMap(mm.getMapRow(), mm.getMapColumn(), mm);
    }
  }

  public void exportMap(File f) throws IOException
  {
    MapModel mm=new MapModel(oc.getRow(), oc.getColumn());
    mm.setTite(oc.getTiteDiv().getData());
    mm.setBlock(oc.getBlockDiv().getData());
    mm.setEvent(oc.getEventDiv().getData());
    mm.setTiteRow(titePicCanvas.getRow());
    mm.setTiteColumn(titePicCanvas.getColumn());
    MUtils.storeMapData(mm, f);
  }
}
